package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.google.common.collect.Lists;
import com.itheima.dao.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @outhor
 * @date 2020/1/15 18:13
 */

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void setNumberByDate(OrderSetting orderSetting) {
        //根据日期查询是否存在预设值,如果存在就更新,如果没有就插入
        Date orderDate = orderSetting.getOrderDate();
        Integer count = orderSettingMapper.findCountByDate(orderDate);
        if (count > 0) {
            //如果存在就更新
            orderSettingMapper.edit(orderSetting);
        } else {
            //不存在就插入
            orderSettingMapper.add(orderSetting);
        }
    }

    @Override
    public List<Map> findOrderSettingByMonth(String date) {
        String startDate = date + "-01";
        String endDate = date + "-31";
        List<OrderSetting> orderSettingList = orderSettingMapper.findOrderSettingByMonth(startDate, endDate);
        //     {"date":22,"month":0,"number":300,"reservations":300,},
        //转换成页面需要的格式
        List<Map> maps = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Date orderDate = orderSetting.getOrderDate();
            Map map = new HashMap();
            map.put("month", orderDate.getMonth());
            map.put("date", orderDate.getDate());
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            maps.add(map);
        }

        return maps;
    }

    @Override
    public void batchSaving(List<OrderSetting> orderSettings) {
        //方法二、启用多线程插入
        //分50个一组将list拆分
        List<List<OrderSetting>> partition = Lists.partition(orderSettings, 50);
        //使用多线程导入
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (List<OrderSetting> orderSettingList : partition) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    for (OrderSetting orderSetting : orderSettingList) {
                        setNumberByDate(orderSetting);
                    }
                }
            });
        }

        //方法一、单线程插入
//        for (OrderSetting orderSetting : orderSettings) {
//            setNumberByDate(orderSetting);
//        }

    }

    @Override
    public void doClearBookingData() {
        Calendar calendar = Calendar.getInstance();
        String nowTime = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        orderSettingMapper.doClearBookingData(nowTime);
    }
}
